package com.sgb.kjwl.repository.impl;

import android.app.Application;

import com.sgb.kjwl.model.api.ApiSource;
import com.sgb.kjwl.model.api.PreferenceSource;
import com.swgk.core.view.entity.MenuEntity;
import com.sgb.kjwl.model.entity.ParentAreaEntity;
import com.sgb.kjwl.model.entity.SearchMailEntity;
import com.sgb.kjwl.model.entity.ShopImEntity;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.repository.BaseRepertory;
import com.swgk.core.base.api.BaseApiSource;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.preference.BasePreferenceSource;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class BaseRepertoryImpl implements BaseRepertory {
    private ApiSource apiSource;
    private PreferenceSource preferenceSource;
    private Application application;

    public BaseRepertoryImpl(BaseApiSource apiSource, BasePreferenceSource preferenceSource) {
        this.apiSource = (ApiSource) apiSource;
        this.preferenceSource = (PreferenceSource) preferenceSource;
    }

    public BaseRepertoryImpl(BaseApiSource apiSource, BasePreferenceSource preferenceSource, Application application) {
        this.apiSource = (ApiSource) apiSource;
        this.preferenceSource = (PreferenceSource) preferenceSource;
        this.application = application;
    }


    public BaseRepertoryImpl(BaseApiSource apiSource) {
        this.apiSource = (ApiSource) apiSource;
    }

    public <T> ObservableTransformer<T, T> rxSchedulerHelper() {
        return new ObservableTransformer<T, T>() {
            @Override
            public ObservableSource<T> apply(Observable<T> upstream) {
                return upstream
//                        .retryWhen(new Function<Observable<Throwable>, ObservableSource<?>>() {
//                            @Override
//                            public ObservableSource<?> apply(Observable<Throwable> throwableObservable) throws Exception {
//                                return throwableObservable.flatMap(new Function<Throwable, ObservableSource<?>>() {
//                                    @Override
//                                    public ObservableSource<?> apply(Throwable throwable) throws Exception {
//                                        if (throwable instanceof TokenExpiredException) {
//                                            return apiSource.refreshToken(preferenceSource.getRefreshToken())
//                                                    .doOnNext(new Consumer<BaseEntity<TokenEntity>>() {
//                                                        @Override
//                                                        public void accept(BaseEntity<TokenEntity> tokenEntityBaseEntity) throws Exception {
//                                                            saveToken(tokenEntityBaseEntity.getData().getToken());
//                                                        }
//                                                    });
//                                        }
//                                        return Observable.error(throwable);
//                                    }
//                                });
//                            }
//                        })
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread());
            }
        };
    }

    @Override
    public Observable<BaseEntity<List<AddressEntity<List<AddressEntity>>>>> getCommonAddress() {
        return apiSource.getCommonAddress().compose(rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<ShopImEntity>> getShopImInfo(String shopId) {
        return apiSource.getShopImInfo(shopId).compose(rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<List<SearchMailEntity>>> searchMailList(String condition) {
        return apiSource.searchMailList(condition).compose(rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<List<MenuEntity<MenuEntity<MenuEntity>>>>> getCategoryData(String areaCode) {
        return apiSource.getCategoryData(areaCode).compose(rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>> getAreaSecondAndThird(String areacode) {
        return apiSource.getAreaSecondAndThird(areacode).compose(rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<List<DictEntity3>>> getAreaSecond(String areaCode) {
        return apiSource.getAreaSecond(areaCode).compose(rxSchedulerHelper());
    }

    @Override
    public Observable<BaseEntity<List<DictEntity3>>> getAreaSecond2(String areaCode) {
        return apiSource.getAreaSecond2(areaCode).compose(rxSchedulerHelper());
    }

    @Override
    public void saveToken(String token) {
        preferenceSource.setToken(token);
    }

    @Override
    public String getToken() {
        return preferenceSource.getToken();
    }

    @Override
    public void saveAccount(String account) {
        preferenceSource.setAccount(account);
    }

    @Override
    public String getAccount() {
        return preferenceSource.getAccount();
    }

    @Override
    public String getJwt() {
        return preferenceSource.getJwt();
    }

    @Override
    public void saveJwt(String jwt) {
        preferenceSource.setJwt(jwt);
    }

    @Override
    public String getNo() {
        return preferenceSource.getNo();
    }

    @Override
    public void saveNo(String no) {
        preferenceSource.setNo(no);
    }

    @Override
    public String getAreaCode() {
        return preferenceSource.getAreaCode();
    }

    @Override
    public void setAreaCode(String code) {
        preferenceSource.setAreaCode(code);
    }

    @Override
    public String getAreaName() {
        return preferenceSource.getAreaName();
    }

    @Override
    public void setAreaName(String name) {
        preferenceSource.setAreaName(name);
    }

    @Override
    public String getLongititude() {
        return preferenceSource.getLongititude();
    }

    @Override
    public void setLongititude(String longititude) {
        preferenceSource.setLongititude(longititude);
    }

    @Override
    public String getLatitude() {
        return preferenceSource.getLatitude();
    }

    @Override
    public void setLatitude(String latitude) {
        preferenceSource.setLatitude(latitude);
    }

    @Override
    public String getLocalCode() {
        return preferenceSource.getLocalCode();
    }

    @Override
    public void setLocalCode(String code) {
        preferenceSource.setLocalCode(code);
    }

    @Override
    public boolean getIsLocal() {
        return preferenceSource.getLocalCode().equals(preferenceSource.getAreaCode());
    }

}
